package com.dream.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.ShareMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.Share;
import com.dream.pojo.ShareExample;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Limit_able;

/*
 * 最近分享的业务类
 */
@Service("Share_impl")
public class Share_impl implements 
All_Service_able<Share>
,Limit_able<Share>{

	/*
	 * 最近分享表的DAO代理对象
	 */
	@Resource(name="shareMapper")
	private ShareMapper share_mapper;

	/*
	 * (查询指定位置和数目的分享记录)
	 * @see com.dream.service.able.Limit_able#list_Limit(java.lang.String[])
	 */
	@Override
	public List<Share> list_Limit(String... str) {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		ShareExample example = new ShareExample();
		//设置排序规则
		example.setOrderByClause("shid desc");
		//设置查询开始位置
		example.setOffset(Integer.parseInt(str[0]));
		//设置查询数目
		example.setLimit(Integer.parseInt(str[1]));
		//创建查询条件工具内部类
		ShareExample.Criteria criteria = example.createCriteria();
		//设置查询条件，state=1
		criteria.andStateEqualTo(1);
		//返回结果集
		return share_mapper.selectByExample(example);
	}

	/*
	 * (添加一条分享，传入，链接，标题)
	 * @see com.dream.service.able.All_Service_able#insert(java.lang.String[])
	 */
	@Override
	public Share insert(String... str) {
		//切换数据库，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建分享对象
		Share share = new Share();
		//插入分享的链接
		share.setLink(str[0]);
		//插入标题
		share.setTitle(str[1]);
		//插入状态
		share.setState(1);
		//写入数据库
		return share_mapper.insertSelective(share)>0?share:null;
	}

	@Override
	public List<Share> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Share getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}

}
